<?php

	class AccountController extends BaseController {

		public function getIndex() {
			return View::make('inscription.index');
		}

		public function postIndex() {
			$rules = array(
				'username'         => 'required|between:3,32|unique:users,username',
				'email'            => 'required|email|unique:users,email',
				'email-confirm'    => 'required|email|same:email',
				'passwd'         => 'required|between:3,32',
				'passwd-confirm' => 'required|same:passwd'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);
			}

			$password = Hash::make(Input::get('passwd'));

			$user = new User;
			$user->username = Input::get('username');
			$user->password = $password;
			$user->email = Input::get('email');
			$user->save();

			// Hack PHPBB

			define('IN_PHPBB', TRUE);

			include($_SERVER['DOCUMENT_ROOT']."../../forum/includes/functions.php");
			include($_SERVER['DOCUMENT_ROOT']."../../forum/includes/db/mysql.php");
			
			global $config, $db;
			$db	= new dbal_mysql();
			define("CONFIG_TABLE", "paradize-forum.phpbb_config");
			$randSeed = DB::table("paradize-forum.phpbb_config")->where("config_name", "=", "rand_seed")->first();
			$config['rand_seed'] = $randSeed->config_value;
			$randSeedLastUpdate = DB::table("paradize-forum.phpbb_config")->where("config_name", "=", "rand_seed")->first();
			$config['rand_seed_last_update'] = $randSeedLastUpdate->config_value;
			$pw_hash = phpbb_hash(Input::get('passwd'));
			
			$phpBBUser = new PhpBBUser;
			$phpBBUser->group_id = 2;
			$phpBBUser->user_ip = "";
			$phpBBUser->user_regdate = time();
			$phpBBUser->username = Input::get('username');
			$phpBBUser->username_clean = strtolower(Input::get('username'));
			$phpBBUser->user_password = $pw_hash;
			$phpBBUser->user_passchg = time();
			$phpBBUser->user_pass_convert = 0;
			$phpBBUser->user_email= Input::get('email');
			$phpBBUser->save();
			
			$uid = $phpBBUser->user_id;
			$phpBBUserGroup = new PhpBBUserGroup;
			$phpBBUserGroup->group_id = 2;
			$phpBBUserGroup->user_id = $uid;
			$phpBBUserGroup->group_leader = 0;
			$phpBBUserGroup->user_pending = 0;
			$phpBBUserGroup->save();

			$phpBBUserGroup = new PhpBBUserGroup;
			$phpBBUserGroup->group_id = 7;
			$phpBBUserGroup->user_id = $uid;
			$phpBBUserGroup->group_leader = 0;
			$phpBBUserGroup->user_pending = 0;
			$phpBBUserGroup->save();

			// END
			
			$data = array(
					'username' => Input::get('username'),
					'password' => Input::get('passwd'),
					'salt' => Hash::make(Input::get('username'))
				);

			Mail::send("emails.welcome", $data, function($message)
			{
				$message->to(Input::get('email'), Input::get('username'))->subject('Création de votre compte sur paradize.org');
			});
			
			return View::make('inscription.activated');
		}

		public function getValidation($account = null) {
			$user = User::where('username', '=', $account);
			if (Hash::check($account, Input::get('salt')) && $user->first()->status == 0) {
				
				$user->update(array('status' => 1));

				return View::make('inscription.activated');
			}
			else {
				return Redirect::route("index","index.html");
			}
		}

		public function getCreateGameAccount() {
			if(User::find(Auth::user()->id)->toL2j()->count() == 0) {
				return View::make('inscription.form');
			}
			else {
				return Redirect::back();
			}
		}

		public function getChar() {
			$chars = Characters::where('user_id', '=', Auth::user()->id)->orderBy('char_name', 'ASC')->get();
			return View::make('characters.index', compact('chars'));
		}

		public function postChar() {
			if(Auth::check()) 
			{
				$rules = array(
					'charname' => 'required|between:2,255|unique:characters,char_name',
					'charlevel' => 'required|between:1,2',
					'charclass' => 'required',
					'chartype' => 'required'
				);

				$validator = Validator::make(Input::all(), $rules);

				if ($validator->fails()) 
				{
					return Redirect::back()->withInput()->withErrors($validator);
				}
				else 
				{
					$characters = new Characters;
					$characters->char_name = htmlentities(Input::get('charname'));
					$characters->char_level = htmlentities(Input::get('charlevel'));
					$characters->char_class = htmlentities(Input::get('charclass'));
					$characters->char_type = htmlentities(Input::get('chartype'));
					$characters->user_id = Auth::user()->id;
					$characters->save();

					$chars = Characters::where('user_id', '=', Auth::user()->id)->orderBy('char_name', 'ASC')->get();
					return View::make('characters.index', compact('chars'));
				}
			}
		}
	}
